<?php
/**
 * 应用初始生成
 * @copyright 2020-2021 WillPHP
 * @author NoMind<24203741@qq.com/113344.com>
 * @version WillPHPv3
 * @since 2021-12-01
 */
namespace wiphp;
defined('WIPHP_URI') or die('Access Denied');
class Build {
	public static function app() {
		$lock = WIPHP_URI.'/app/build.lock';
		if (!is_writable($lock)) {
			if (!touch($lock)) {
				exit('应用目录不可写，请确认app目录存在，并设置权限可写！');
			}
			//目录生成
			if (!is_dir(PATH_RUNTIME)) mkdir(PATH_RUNTIME, 0777, true);
			if (!is_dir(APP_PATH)) mkdir(APP_PATH, 0755, true);	
			$viewpath = (THEME_ON)? PATH_VIEW.'/default' : PATH_VIEW;
			$dir = [];
			$dir[] = APP_PATH.'/config';
			$dir[] = APP_PATH.'/controller';
			$dir[] = APP_PATH.'/model';
			$dir[] = APP_PATH.'/widget';
			$dir[] = APP_PATH.'/validate';
			$dir[] = $viewpath.'/public';
			$dir[] = $viewpath.'/index';
			$dir[] = $viewpath.'/abc';
			$dir[] = WIPHP_URI.'/app/common/model';
			foreach ($dir as $dr) {
				if(!is_dir($dr)) mkdir($dr, 0755, true);
			}	
			//自定义函数文件
			if (!file_exists(WIPHP_URI.'/app/common.php')) {
				file_put_contents(WIPHP_URI.'/app/common.php', "<?php\n//自定义函数文件");
			}
			//演示路由
			$route = "<?php\nreturn [\n\t'demo' => 'abc/abc',\n\t'demo_p(:page)' => 'abc/abc/p/\${1}',\n];";
			file_put_contents(APP_PATH.'/config/route.php', $route);	
			//模板初始化
			$t_index = file_get_contents(WIPHP_PATH.'/tpl/index.tpl'); //首页模板
			file_put_contents($viewpath.'/index/index.html', $t_index); 			
			$t_jump = file_get_contents(WIPHP_PATH.'/tpl/jump.tpl'); //转跳模板
			file_put_contents($viewpath.'/public/jump.html', $t_jump);			
			$t_abc = file_get_contents(WIPHP_PATH.'/tpl/abc.tpl'); //演示模板
			file_put_contents($viewpath.'/abc/abc.html', $t_abc);
			//控制器初始化
			$namespace = APP_NAMESPACE;		
			//默认控制器				
			$c_index = "<?php\nnamespace $namespace\\controller;\nclass IndexController{\n\tpublic function index(){\n\t\treturn view();\n\t}\n}";
			file_put_contents(APP_PATH.'/controller/IndexController.php', $c_index);
			//基控制器
			$c_base = "<?php\nnamespace $namespace\\controller;\nabstract class BaseController{\n\tuse \\wiphp\\Jump;\n\tpublic function __construct(){}\n}";
			file_put_contents(APP_PATH.'/controller/BaseController.php', $c_base);
			//API控制器
			$c_api = "<?php\nnamespace $namespace\\controller;\nclass ApiController{\n\tuse \\wiphp\\Jump;\n\tpublic function captcha(){\n\t\treturn extend('captcha')->create();\n\t}\n\tpublic function clear(){\n\t\tcache(null);\n\t\t\$this->_jump('清除缓存成功', 1, 'index/index');\n\t}\n}";
			file_put_contents(APP_PATH.'/controller/ApiController.php', $c_api);
			//演示Abc控制器
			$c_abc = "<?php\nnamespace $namespace\\controller;\nclass AbcController extends BaseController{\n\tpublic function abc(){\n\t\treturn view();\n\t}\n}";
			file_put_contents(APP_PATH.'/controller/AbcController.php', $c_abc);
			//空控制器和空操作
			$c_empty = "<?php\nnamespace $namespace\\controller;\nclass EmptyController{\n\tuse \\wiphp\\Jump;\n\tpublic function empty(){\n\t\t\$this->error(__MODULE__.'->'.__ACTION__.'() 不存在。');\n\t}\n}";
			file_put_contents(APP_PATH.'/controller/EmptyController.php', $c_empty);
			//演示模型生成
			$m_abc = "<?php\nnamespace $namespace\\model;\nclass AbcModel{\n\tpublic function abc(){\n\t\treturn 'abc';\n\t}\n}";
			file_put_contents(APP_PATH.'/model/AbcModel.php', $m_abc);
			//演示部件生成
			$w_abc = "<?php\nnamespace $namespace\\Widget;\nuse \\wiphp\\Widget;\nclass AbcWidget extends Widget{\n\tprotected \$name = 'abc';\n\tprotected \$time = 0;\n\tpublic function setData(\$id = '', \$options = []){\n\t\treturn 'abc';\n\t}\n}";
			file_put_contents(APP_PATH.'/widget/AbcWidget.php', $w_abc);
			//演示验证器生成
			$v_abc = "<?php\nnamespace $namespace\\validate;\nuse \\wiphp\\Validate;\nclass AbcValidate extends Validate{\n\tprotected \$table = 'abc';\n\tprotected \$noFields = [];\n\tprotected \$allScene = [];\n\tprotected \$rule = [];\n\tprotected \$info = [];\n\tprotected \$auto = [];\n}";
			file_put_contents(APP_PATH.'/validate/AbcValidate.php', $v_abc);
			//公用模型
			$m_common = "<?php\nnamespace app\\common\\model;\nclass AbcModel{\n\tpublic function abc(){\n\t\treturn 'common abc';\n\t}\n}";
			file_put_contents(WIPHP_URI.'/app/common/model/AbcModel.php', $m_common);
			//删除锁定文件
			unlink($lock);
		}
	}
}